package daliytool.script;

import com.alibaba.fastjson.JSONObject;
import daliytool.WebViewMain;
import daliytool.http.server.response.ResultObject;
import daliytool.http.server.support.WebContext;
import daliytool.service.FileService;
import daliytool.vo.TreeVO;
import javafx.application.Platform;
import javafx.stage.DirectoryChooser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.*;

/**
 * 描述 新通教育
 * 处理文件
 *
 * @author ddshuai
 * @date 2018-10-26 11:13
 **/
@SuppressWarnings("ALL")
public class FileFunctionScript {

    private static final Logger logger = LoggerFactory.getLogger(FileFunctionScript.class);

    /**
     * 选择项目跟路径 提供给前端js调用
     *
     * @return String
     */
    public String choseRootDirPath(String sys) {

        String cwd = System.getProperty("user.dir");
        File file = new File(cwd);
        DirectoryChooser chooser = new DirectoryChooser();
        chooser.setTitle("选择根目录");
        chooser.setInitialDirectory(file);
        File chosenDir = chooser.showDialog(WebViewMain.stageNow);
        logger.debug("选择的路径:{}", chosenDir.getAbsolutePath());
        WebContext.setRootPath(chosenDir.getAbsolutePath(), sys);
        return chosenDir.getAbsolutePath();

    }


    /**
     * 退出程序
     */
    public void exit() {
        logger.info("退出程序");
        Platform.exit();
    }

    /**
     * 获取根路径
     *
     * @return
     */
    public String getRootPath() {
        return getJsonString(FileService.getRootPath());
    }

    /**
     * 获取目录结构
     *
     * @param path
     * @param sys
     * @return
     */
    public String getTree(String path, String sys) {
        return getJsonString(FileService.getTree(path, sys));
    }

    /**
     * 保存历史数据
     *
     * @param winCodePath
     * @param linuxCodePath
     * @return
     */
    public String saveHistory(String winCodePath, String linuxCodePath,String sys) {
        Map<String, String> map = new HashMap<>(2);
        map.put("winCodePath", winCodePath);
        map.put("linuxCodePath", linuxCodePath);
        map.put("sys",sys);
        return getJsonString(FileService.saveHistory(map));
    }

    public String queryHistory(){
        return getJsonString(FileService.queryHistory());
    }

    private String getJsonString(Object o) {
        JSONObject jsonRootObj = new JSONObject();
        jsonRootObj.put("data", o);

        return jsonRootObj.toJSONString();
    }

    public static void main(String[] args) {
        System.out.println("WEB-INF" + File.separator + "classes");
    }
}
